<?php
/**
 * Created by PhpStorm.
 * User: hpp
 * Date: 2019/12/6
 * Time: 10:54
 */

namespace gzapi\task\controller;

use think\Controller;
use think\Db;
use api\common\service\FunctionService;
/**
 * 数据同步接口
 * Class TaskController
 * @package api\task\controller
 */
class ProtaskController extends Controller
{
    //获取巡检信息
    public function inspectInfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $xj_azdbh=input('get.xj_azdbh');
        $where=[];
        $where1='';
        if(isset($xj_azdbh)){
            $where1['xj_azdbh']=input('get.xj_azdbh');
        }
        if(isset($start_time)  and isset($end_time)){
            $where='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
            $where='update_time< "'.$end_time.'" AND update_time> "'.$start_time.'"';
        }
        $res=Db::table('cmf_gz_hise_service_inspection')->where($where)->where($where1)->select();
        if(!empty($res)){
            $res=$res->toArray();
            foreach($res as $k=>$v){
                if($res[$k]['create_time']){
                    $res[$k]['create_time']=date('Y-m-d',strtotime($res[$k]['create_time']));//时间转化
                }
                //区分外派和非外派
                if($v['xj_is_wp'])
                {
                    //如果是外派
//                    $res[$k]['xj_dw_name'] = getazsName($res[$k]['xj_dw']);
                    if($v['xj_xjr'])
                    {
                        $res[$k]['xj_xjr_name'] = getazdName($res[$k]['xj_xjr']);
                    }else{
                        $res[$k]['xj_xjr_name'] = '';
                    }
                    $res[$k]['xj_fwgcs_name'] = getgcsName($res[$k]['xj_fwgcs']);
                }else{
                    //如果不是外派
//                    $res[$k]['xj_dw_name'] = getfgsName($res[$k]['xj_dw']);
                    $res[$k]['xj_xjr_name'] = getgcsName($res[$k]['xj_fwgcs']);
                    $res[$k]['xj_fwgcs_name'] = getgcsName($res[$k]['xj_fwgcs']);
                }
                $stepcheck=Db::table('cmf_gz_inspect_step_checking')->where('tid',$v['xj_id'])->select();
                $yanzheng = [];
                if(!empty($stepcheck)){
                    $stepcheck=$stepcheck->toArray();
                    foreach($stepcheck as $ks=>$vs){
                        if($stepcheck[$ks]['create_time']){
                            $stepcheck[$ks]['create_time']=date('Y-m-d',strtotime($stepcheck[$ks]['create_time']));//时间转化

                        }
                        if($stepcheck[$ks]['update_time']){
                            $stepcheck[$ks]['update_time']=date('Y-m-d',strtotime($stepcheck[$ks]['update_time']));//时间转化

                        }
                        $stepcheck[$ks]['inspect_code']=$v['xj_inspectcode'];
                        $yanzheng[] = $stepcheck[$ks]['node'];
                        $res[$k]['stepcheck'] = $this->arraykeyToUPPER($stepcheck);
                    }
                }


                $step=Db::table('cmf_gz_hise_service_inspection_step')->where('jd_xjid',$v['xj_id'])->select();
                if(!empty($step)){
                    $step=$step->toArray();
                   foreach($step as $ks=>$vs){
                     if($step[$ks]['create_time']){
                         $step[$ks]['create_time']=date('Y-m-d',strtotime($step[$ks]['create_time']));//时间转化
                       }
                       $step[$ks]['inspect_code']=$v['xj_inspectcode'];
                       $step[$ks]['jd_sfczwt']=$step[$ks]['jd_yz'];
                       if(in_array($step[$ks]['jd_azjd'],$yanzheng))
                       {
                           $step[$ks]['jd_yz']=1;
                       }else{
                           $step[$ks]['jd_yz']=0;
                       }
                   }

                    $res[$k]['step'] = $this->arraykeyToUPPER($step);
                }



            }
            $res = $this->arraykeyToUPPER($res);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
    //获取调试信息
    public function  proTest(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
        }
        $res=Db::table('cmf_gz_hise_service_pro_test')->where($where)->where($where1)->select();
        if(!empty($res)){
            $res=$res->toArray();
            foreach($res as $k=>$v){
                if($res[$k]['create_time']){
                    $res[$k]['create_time']=date('Y-m-d',$res[$k]['create_time']); //时间转化
                }
                //区分外派和非外派
                if($v['ts_ifwp'])
                {
                    //如果是外派
//                    $res[$k]['ts_dw_name'] = getazsName($res[$k]['ts_dw']);
                    if($v['ts_ry'])
                    {
                        $res[$k]['ts_ry_name'] = getazdName($res[$k]['ts_ry']);
                    }else{
                        $res[$k]['ts_ry_name'] = '';
                    }
                    $res[$k]['ts_fwgcs_name'] = getgcsName($res[$k]['ts_fwgcs']);
                    $res[$k]['created_by_name'] = getgcsName($res[$k]['created_by']);
                    $res[$k]['last_upd_by_name'] = getgcsName($res[$k]['last_upd_by']);

                }else{
                    //如果不是外派
//                    $res[$k]['ts_dw_name'] = getfgsName($res[$k]['ts_dw']);
                    $res[$k]['ts_ry_name'] = getgcsName($res[$k]['ts_fwgcs']);
                    $res[$k]['ts_fwgcs_name'] = getgcsName($res[$k]['ts_fwgcs']);
                    $res[$k]['created_by_name'] = getfgsName($res[$k]['created_by']);
                    $res[$k]['last_upd_by_name'] = getgcsName($res[$k]['last_upd_by']);
                }
              $detail=Db::table('cmf_gz_hise_service_pro_test_detail')->where('ts_xjid',$v['ts_code'])->select();
                if(!empty($detail)){
                    $detail=$detail->toArray();
                    foreach($detail as $ke=>$v){
                        if($detail[$ke]['create_time']){
                           $detail[$ke]['create_time']=date('Y-m-d',$detail[$ke]['create_time']); //时间转化
                        }
                        $detail[$ke]['ts_tsid']= $detail[$ke]['ts_xjid'];
                        unset($detail[$ke]['ts_xjid']);
                    }
                    $res[$k]['detail'] = $this->arraykeyToUPPER($detail);
                }
            }
            $res = $this->arraykeyToUPPER($res);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
    //获取培训信息
    public function trainInfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
        }
        $res=Db::table('cmf_gz_hise_service_pro_train')->where($where)->where($where1)->select();
        if(!empty($res)){
            $res=$res->toArray();
            foreach($res as $k=>$v){
                if($res[$k]['create_time']){
                    $res[$k]['create_time']=date('Y-m-d',$res[$k]['create_time']);//时间转化
                }
                $res[$k]['created_by_name'] = getgcsName($res[$k]['created_by']);
                if($res[$k]['last_upd_by'])
                {
                    $res[$k]['last_upd_by_name'] = getgcsName($res[$k]['last_upd_by']);
                }else{
                    $res[$k]['last_upd_by_name'] = '';
                }
                $detail=Db::table('cmf_gz_hise_service_pro_train_detail')->where([
                    'azd_id'=>$v['azd_id'],
                    'train_id'=>$v['row_id']
                ])->select();
                if(!empty($detail)){
                    $detail=$detail->toArray();
                    foreach($detail as $key=>$val){
                        if($detail[$key]['create_time']){
                            $detail[$key]['create_time']=date('Y-m-d',$detail[$key]['create_time']);//时间转化
                        }
                      
                        $detail[$key]['training_type']='KG';
                    }
                    $res[$k]['detail'] = $this->arraykeyToUPPER($detail);      
                }
            }
            $res = $this->arraykeyToUPPER($res);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
    //获取验收记录
    public function acceptInfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
    }
        $res=Db::table('cmf_gz_hise_service_pro_accept')->where($where)->where($where1)->select();
        if(!empty($res)){
            $res=$res->toArray();
            foreach($res as $ktime=>$vtime){
                if($res[$ktime]['create_time']){
                    $res[$ktime]['create_time']=date('Y-m-d',$res[$ktime]['create_time']); 
                }
               
            }
            $res=$this->arraykeyToUPPER($res);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
    //获取交付培训
    public function paymentInfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
        }
        $res=Db::table('cmf_gz_hise_service_pro_payment')->where($where)->where($where1)->select();
      
        if(!empty($res)){
          $res=$res->toArray();
          //培训时间处理
          foreach($res as $ktime=>$vtime){
              if($res[$ktime]['create_time']){
                $res[$ktime]['create_time']=date('Y-m-d',$res[$ktime]['create_time']);
              }
              $res[$ktime]['created_by_name'] = getgcsName($res[$ktime]['created_by']);
              if($res[$ktime]['last_upd_by'])
              {
                  $res[$ktime]['last_upd_by_name'] = getgcsName($res[$ktime]['last_upd_by']);
              }
          }
          foreach($res as $k=>$v){
              $detail=Db::table('cmf_gz_hise_service_pro_payment_detail')->where([
                  'azd_id'=>$v['azd_id'],
                  'train_no'=>$v['training_no']
              ])->select();
              if(!empty($detail)){
                  $detail=$detail->toArray();
                  foreach($detail as $key=>$val){
                    if($detail[$key]['create_time']){
                        $detail[$key]['create_time']=date('Y-m-d',$detail[$key]['create_time']);
                    }
                    $detail[$key]['training_type']='JF';
                }
                  $res[$k]['detail'] = $this->arraykeyToUPPER($detail);
              }
          }
            $res = $this->arraykeyToUPPER($res);
           
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
    //获取工程信息
    public function projectInfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
    }
        $res=Db::table('cmf_gz_hise_service_project')->where($where1)->where($where)->select();
        if(!empty($res)){
            $res=$res->toArray();
            foreach($res as $ktime=>$vtime){
                $res[$ktime]['create_time']=$res[$ktime]['create_time']?date('Y-m-d',(int)$res[$ktime]['create_time']):$res[$ktime]['create_time'];
                $res[$ktime]['ycgcwcsj']=$res[$ktime]['ycgcwcsj']?date('Y-m-d',(int)$res[$ktime]['ycgcwcsj']):$res[$ktime]['ycgcwcsj'];
                $res[$ktime]['yyaz_sj']=$res[$ktime]['yyaz_sj']?date('Y-m-d',(int)$res[$ktime]['yyaz_sj']):$res[$ktime]['yyaz_sj'];
                //$res[$ktime]['bx_kssj']=$res[$ktime]['bx_kssj']?date('Y-m-d',(int)$res[$ktime]['bx_kssj']):$res[$ktime]['bx_kssj'];
                $res[$ktime]['tjrq']=$res[$ktime]['tjrq']?date('Y-m-d',(int)$res[$ktime]['tjrq']):$res[$ktime]['tjrq'];
                $res[$ktime]['wgbgqr_sj']=$res[$ktime]['wgbgqr_sj']?date('Y-m-d',(int)$res[$ktime]['wgbgqr_sj']):$res[$ktime]['wgbgqr_sj'];
                $res[$ktime]['tssj']=$res[$ktime]['tssj']?date('Y-m-d',(int)$res[$ktime]['tssj']): $res[$ktime]['tssj'];
                $res[$ktime]['wgsj']=$res[$ktime]['wgsj']?date('Y-m-d',(int)$res[$ktime]['wgsj']):$res[$ktime]['wgsj'];
                $res[$ktime]['yyjc_sj']=$res[$ktime]['yyjc_sj']?date('Y-m-d',(int)$res[$ktime]['yyjc_sj']):$res[$ktime]['yyjc_sj'];
                $res[$ktime]['sjjc_sj']=$res[$ktime]['sjjc_sj']?date('Y-m-d',(int)$res[$ktime]['sjjc_sj']):$res[$ktime]['sjjc_sj'];
                $res[$ktime]['jlcj_sj']=$res[$ktime]['jlcj_sj']?date('Y-m-d',(int)$res[$ktime]['jlcj_sj']):$res[$ktime]['jlcj_sj'];
                $res[$ktime]['jlgx_sj']=$res[$ktime]['jlgx_sj']?date('Y-m-d',(int)$res[$ktime]['jlgx_sj']):$res[$ktime]['jlgx_sj'];
//                $res[$ktime]['first_delivery_time']= $res[$ktime]['first_delivery_time']?date('Y-m-d',(int)$res[$ktime]['first_delivery_time']): $res[$ktime]['first_delivery_time'];
                $res[$ktime]['bx_kssj']=$res[$ktime]['bx_kssj']?date('Y-m-d',(int)$res[$ktime]['bx_kssj']):$res[$ktime]['bx_kssj'];
                $zjbxq = $res[$ktime]['zjbxq'];
                $zybjbxq = $res[$ktime]['zybjbxq'];
                $bx_kssj = $res[$ktime]['bx_kssj'];
                if($res[$ktime]['bx_kssj'])
                {
                    $res[$ktime]['zjbxq_end'] = date('Y-m-d',strtotime("$bx_kssj + $zjbxq month"));
                    $res[$ktime]['zybjbxq_end'] = date('Y-m-d',strtotime("$bx_kssj + $zybjbxq month"));
                }else{
                    $res[$ktime]['zjbxq_end'] = '';
                    $res[$ktime]['zybjbxq_end'] = '';
                }
            }
            $res=$this->arraykeyToUPPER($res);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }

    /**
     * 发货时间修改
     */
    public function updateSendTime()
    {
        $first_delivery_time=input('param.first_delivery_time');
        $azd_id=input('param.azd_id');
        if(!$first_delivery_time)
        {
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'时间不能为空']);
        }
        if(!$azd_id)
        {
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'安装单id不能为空']);
        }
        //判断安装单是否存在，是否是未开工和安装状态
        $res=Db::table('cmf_gz_hise_service_project')->where('azd_id',$azd_id)->find();
        if(!$res)
        {
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'安装单不存在']);
        }
        if($res['gczt'] == 5)
        {
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'安装单已经运行结束']);
        }
        $result = Db::table('cmf_gz_hise_service_project')->where('azd_id',$azd_id)->update(['first_delivery_time'=>$first_delivery_time,'gczt'=>7,'update_time'=>time()]);
        if($result)
        {
            return json(['CODE'=>1,'DATA'=>'','MASSAGE'=>'安装单更新发货时间成功']);
        }else{
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'安装单更新发货时间失败']);
        }
    }
    /**
     * 将数组中，小写的键名转换为大写
     * @param []
     * @return []
     * @date 2019/12/16 1:04 PM
     */
    public static function arraykeyToUPPER( $data = array() )
    {
        if( !empty($data) )
        {
            //一维数组
            if(count($data) == count($data,1))
            {
                $data = array_change_key_case($data , CASE_UPPER);
            }
            else    //二维数组
            {
                foreach( $data as $key => $value )
                {
                    $data[$key] = array_change_key_case($value , CASE_UPPER);
                }
            }
        }
        return $data;
    }
   //获取条码
    public function getinstinfo(){
        $start_time=input('get.start_time');
        $end_time=input('get.end_time');
        $azd_id=input('get.azd_id');
        $where=[];
        $where1='';
        if(isset($azd_id)){
            $where['azd_id']=$azd_id;
        }
        if(!$start_time){
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'缺少开始时间参数']);
        }
        if(!$end_time){
            return json(['CODE'=>0,'DATA'=>'','MASSAGE'=>'缺少结束时间参数']);
        }
        if(isset($start_time)  and isset($end_time)){
            $where1='update_time<'.strtotime($end_time).' AND update_time>'.strtotime($start_time);
        }
        $res=Db::table('cmf_gz_hise_service_pro_inst_detail')->where($where1)->where($where)->field('hise_service_pro_inst_detail_id,azd_id,xh,jqxlh,sjfhsj,xhdm,num,chht,chjxs,zpdz,smsj,sfcf,cfazdh,jxcg_bs,status,create_time,update_time')->select()->toArray();
        $res=$this->arraykeyToUPPER($res);
        foreach($res as $key=>&$val){
            $val['CREATE_TIME']=date("Y-m-d H:i:s",$val['CREATE_TIME']);
            $val['UPDATE_TIME']=date("Y-m-d H:i:s",$val['UPDATE_TIME']);
            $val['SJFHSJ']=date("Y-m-d",$val['SJFHSJ']);
            $val['SMSJ']=date("Y-m-d H:i:s",$val['SMSJ']);
        }
        return json(['CODE'=>1,'DATA'=>$res]);
    }
}
